package com.spt.springbootzhsx.service.suggest.Get_UserWatchList_4;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class GetMWL_Reducer extends Reducer<Text, Text, Text, Text> {
    private Text outV = new Text();
    private Map<String, Float> map = new HashMap<>();
    private StringBuffer sb = new StringBuffer();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {

        for (Text value : values) {
            String[] vals=value.toString().split("\t");
            if (map.containsKey(vals[0])) {
                map.put(vals[0], map.get(vals[0]) + Float.parseFloat(vals[1]));
            } else {
                map.put(vals[0], Float.parseFloat(vals[1]));
            }
        }

        for (Map.Entry<String, Float> en : map.entrySet()) {
            sb.append(en.getKey()).append(":").append(en.getValue()).append(",");
        }
        sb.setLength(sb.length()-1);
        outV.set(sb.toString());
        context.write(key,outV);

        //重置数据
        sb.setLength(0);
        map.clear();
        outV.clear();

    }
}
